package com.drojian;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;


import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private OkHttpClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        client = new OkHttpClient();
    }

    public void getSync(View view) {
        new Thread() {
            @Override
            public void run() {
                Request request = new Request.Builder().url("https://wanandroid.com/harmony/index/json").build();
                Call call = client.newCall(request);
                try {
                    Response response = call.execute();
                    Log.d("xxx", response.body().string());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
    }

    public void getAsync(View view) {
        new Thread() {
            @Override
            public void run() {
                Request request = new Request.Builder().url("https://wanandroid.com/harmony/index/json?a=1&b=2").get().build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        Log.d("xxx", response.body().string());
                    }
                });

            }
        }.start();
    }

    public void postSync(View view) {
        new Thread() {
            @Override
            public void run() {
                FormBody formBody = new FormBody.Builder().add("a", "1").add("b", "2").build();
                Request request = new Request.Builder().url("https://wanandroid.com/harmony/index/json").post(formBody).build();
                Call call = client.newCall(request);
                try {
                    Response response = call.execute();
                    Log.d("xxx", response.body().string());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        }.start();
    }

    public void postAsync(View view) {
        new Thread() {
            @Override
            public void run() {
                FormBody formBody = new FormBody.Builder().add("a", "1").add("b", "2").build();
                Request request = new Request.Builder().url("https://wanandroid.com/harmony/index/json").post(formBody).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback(){

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        Log.d("xxx", response.body().string());
                    }

                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }
                });

            }
        }.start();
    }
}